//
//  AllocatedCtry.m
//  ZHZtest
//
//  Created by 李彦宏 on 2018/1/12.
//  Copyright © 2018年 LYH. All rights reserved.
//已分配研究生的学员
#import "AllocatedCtry.h"
#import "DistributStuCell.h"//分配cell
#import "DistributStuHead.h"//分配head
#import "JWChooseGrSeachVC.h"//选择研究生搜索
#import "JWUserManager.h"
#import "SDUserModel.h"
#import "JWGRUsermodel.h"
#import "LZPickViewManager.h"


@interface AllocatedCtry ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *Allocatedtab;
@property (nonatomic, strong) SDUserModel *SDmodel;
@property (nonatomic, strong) JWGRUsermodel *Gmodel;
@property (nonatomic, strong) NSMutableArray *SDdislist;
@property (nonatomic, strong) NSMutableArray *GRarrylist;
@end

@implementation AllocatedCtry
//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-(Navigation_Bar_Height-44)) style:UITableViewStylePlain];
    
   
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
  
    
tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tableView];
    self.Allocatedtab=tableView;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

     [self setdataDistribut];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    [self setupTable];

    [self setGRdate];
  
}


#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _SDdislist.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *CellIdentifier=[NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
    DistributStuCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    NSLog(@"----%@", cell);
  
    if(cell==nil){
        cell=[[DistributStuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [cell setChooseGRHandle:^{
        JWChooseGrSeachVC *vc=[[JWChooseGrSeachVC alloc]init];
        
        vc.tagsArray=[[NSMutableArray alloc]init];
        if(self.GRarrylist.count<7){
           vc.tagsArray=[NSMutableArray arrayWithObjects:@"李彦宏",@"朱圳",@"严戈闲",@"赵延",@"田涛",@"肖欢欣",@"游鹤亭",@"陈炽", nil];
            
        }else{
            for (int i=0; i<7; i++) {
                self.Gmodel=self.GRarrylist[i];
                [vc.tagsArray  addObject:self.Gmodel.name];
            }
        }
        
        self.SDmodel=self.SDdislist[indexPath.row];
        [JWUserManager saveSD:self.SDmodel];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
     cell.model = self.SDdislist[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.Allocatedtab deselectRowAtIndexPath:indexPath animated:YES];
     self.SDmodel=self.SDdislist[indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 120;
}
//#pragma -mark 尾部的内容
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, 120)];
    footer.backgroundColor=[UIColor whiteColor];


    return footer;
}
#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    DistributStuHead *head=[[DistributStuHead alloc]init];
    
    return head;
  
}

#pragma -mark ————————————获取学生分配列表————————————————————
-(void)setdataDistribut{
    NSDictionary *dict=@{
                         @"jwid":JWid,
                         @"jwpassword":JWpassword,
                         };
    [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_SdistriList parmas:dict successData:^(id json) {
         [self.SDdislist removeAllObjects];
  //      NSLog(@"->>-json--%@", json);
        NSArray *arry=json[@"data"];
        __weak typeof(self) weakslf = self;
        for (int i=0; i<arry.count;i++) {
            self.SDmodel=[SDUserModel yy_modelWithJSON:arry[i]];
            //必须要用weakslf，避免重复引用
            [weakslf.SDdislist addObject:weakslf.SDmodel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if(arry.count==0){
                [self.Allocatedtab removeFromSuperview];
                KongDataView  *Kview=[KongDataView NilDataview];
                self.view.frame= Kview.frame;
                [self.view addSubview:Kview];
            }
            
            [weakslf.Allocatedtab reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    } ];
}

#pragma -mark ————————————————研究生数据————————————————————
-(void)setGRdate{
    
    __weak typeof(self) weakslf = self;
    
    NSDictionary *dict=@{
                         @"jwid":JWid,
                         @"jwpassword":JWpassword,
                         @"page" :@"1",
                         };
//    NSLog(@"--dict--%@", dict);
    //    参数  分页=>page 教务id=〉jwid ，秘码=>jwpassword
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_ChangeGR parame:dict successData:^(id json) {
        [self.GRarrylist removeAllObjects];
        NSArray * arr = json[@"data"];
        
        for(int i=0;i<arr.count;i++){
            
            self.Gmodel = [JWGRUsermodel yy_modelWithJSON:arr[i]];
            [JWUserManager saveGR:self.Gmodel];
            
            [weakslf.GRarrylist addObject: self.Gmodel ];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.Allocatedtab reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
}
-(NSMutableArray * )GRarrylist{
    if(_GRarrylist==nil){
        _GRarrylist=[[NSMutableArray alloc]init];
    }
    return _GRarrylist;
}
-(NSMutableArray *)SDdislist{
    if(!_SDdislist){
        _SDdislist=[[NSMutableArray alloc]init];
    }
    return _SDdislist;
}

@end
